package com.tanhua.admin.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.itheima.commons.utils.Constants;
import com.tanhua.admin.service.AdminService;
import com.tanhua.model.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/system/users")
public class SystemController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //获取验证码图片
    @GetMapping("/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {

        //获取图片验证码
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(299, 97);
        String code = captcha.getCode();
        captcha.setBackground(Color.green);
        //添加到redis中
        redisTemplate.opsForValue().set(Constants.CAP_CODE + uuid, code, 5, TimeUnit.MINUTES);
        captcha.write(response.getOutputStream());
    }

    /**
     * 登录校验
     * @param map
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        Map retMap = adminService.login(map);
        return ResponseEntity.ok(retMap);
    }

    /**
     * 查询用户信息
     * @return
     */
    @PostMapping("/profile")
    public ResponseEntity profile() {
        AdminVo vo = adminService.profile();
        return ResponseEntity.ok(vo);
    }



}
